import axios from 'axios'
import router from '@/router'
import { ElMessage } from 'element-plus'
import {getToken,removeToken,removeNickName, setToken} from '@/utils/auth'



axios.defaults.baseURL = process.env.VUE_APP_BASE_API_URL
axios.defaults.timeout = 50000
axios.defaults.withCredentials = true
axios.defaults.headers['X-Requested-With'] = 'XMLHttpRequest'
axios.defaults.headers.post['Content-Type'] = 'application/json'
axios.defaults.headers['tenant-id'] = 1 //默认租户是1
//访问设置
axios.interceptors.request.use(config => {
  // 设置 token
  if (getToken()) {
    config.headers['Authorization']  = 'Bearer ' + getToken()// 让每个请求携带自定义token
  }else{
    config.headers['Authorization']  = 'Bearer test1'  // 让每个请求携带自定义token
  }
  return config
}, error => {
    Promise.reject(error)
})

//响应设置
axios.interceptors.response.use(res => {
  if (typeof res.data !== 'object') {
    ElMessage.error('服务端异常！')
    return Promise.reject(res)
  }
  return res
}, error => {
  ElMessage.error('网络异常！')
  Promise.reject(error)
})

export default axios